<template>
  <div class="login-container">
    <div class="login-header">
      <h1 class="app-title">我的项目</h1>
      <button class="login-switch-btn" @click="$router.push('/login-code')">
        验证码登录
      </button>
    </div>

    <form @submit.prevent="handleLogin" class="login-form">
      <div class="form-item">
        <label>账号:</label>
        <input 
          type="text" 
          v-model="form.username" 
          placeholder="请输入您的账号" 
          required
        />
      </div>

      <div class="form-item">
        <label>密码:</label>
        <div class="password-input">
          <input 
            :type="showPassword ? 'text' : 'password'" 
            v-model="form.password" 
            placeholder="请输入密码" 
            required
          />
          <button 
            type="button" 
            class="eye-icon" 
            @click="showPassword = !showPassword"
          >
            👁
          </button>
        </div>
      </div>

      <button type="submit" class="login-button" :disabled="loading">
        {{ loading ? '登录中...' : '登录' }}
      </button>

      <div class="form-links">
        <router-link to="/register" class="link-text">注册</router-link>
        <span class="link-text forget-password">忘记密码?</span>
      </div>
    </form>

    <div class="other-login">
      <p class="other-title">其他登录方式</p>
      <div class="social-icons">
        <div 
          class="social-icon dingtalk" 
          @click="loginWithDingtalk"
          title="钉钉登录"
        >
          <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="32" height="32">
            <path fill="#1677FF" d="M213.76 512 0 0l426.24 213.76 0.02 384L213.76 512zM810.24 213.76 597.76 0l0 384 85.333333-43.946667L810.24 810.24zM810.24 810.24 597.76 1024l0-384 85.333333-43.946667L810.24 810.24z"/>
          </svg>
        </div>
        <div class="social-icon">🐧</div>
        <div class="social-icon">🌐</div>
        <div class="social-icon">🍎</div>
      </div>
    </div>

    <div class="terms">
      <span class="checkbox">☐</span>
      <span class="terms-text">
        注册即代表你同意
        <a href="#" class="link">《我的协议》</a>
        <a href="#" class="link">《隐私保护指引》</a>
        <a href="#" class="link">《中国移动认证服务条款》</a>
      </span>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/api'

const router = useRouter()
const loading = ref(false)
const showPassword = ref(false)

const form = ref({
  username: '',
  password: ''
})

const handleLogin = async () => {
  loading.value = true
  try {
    const response = await api.post('/auth/login', {
      username: form.value.username,
      password: form.value.password
    })

    // 保存token和refresh_token
    const { access_token, refresh_token, user_id, username } = response.data
    localStorage.setItem('token', access_token)
    if (refresh_token) localStorage.setItem('refresh_token', refresh_token)  // 保存Refresh Token用于续命
    localStorage.setItem('user_id', user_id)
    localStorage.setItem('username', username)

    alert('登录成功！')
    router.push('/interest-selection')
  } catch (error) {
    alert(error.response?.data?.detail || '登录失败')
  } finally {
    loading.value = false
  }
}

const loginWithDingtalk = () => {
  router.push('/dingtalk-login')
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  min-height: 100vh;
  background: #fff;
  padding: 20px;
}

.login-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.app-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.login-switch-btn {
  background: #007AFF;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
}

.login-form {
  max-width: 400px;
  margin: 0 auto;
}

.form-item {
  margin-bottom: 24px;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  font-size: 16px;
  color: #333;
}

.form-item input {
  width: 100%;
  padding: 12px;
  border: none;
  border-bottom: 1px solid #e0e0e0;
  font-size: 16px;
  outline: none;
}

.password-input {
  display: flex;
  align-items: center;
  gap: 10px;
}

.password-input input {
  flex: 1;
}

.eye-icon {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
}

.login-button {
  width: 100%;
  padding: 16px;
  background: #007AFF;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  margin-top: 20px;
}

.login-button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.form-links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.link-text {
  color: #333;
  text-decoration: none;
}

.forget-password {
  color: #007AFF;
}

.other-login {
  text-align: center;
  margin-top: 60px;
}

.other-title {
  color: #999;
  font-size: 14px;
  margin-bottom: 20px;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.social-icon {
  width: 50px;
  height: 50px;
  background: #f5f5f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  cursor: pointer;
  transition: all 0.3s;
}

.social-icon:hover {
  background: #e8e8e8;
  transform: scale(1.1);
}

.social-icon.dingtalk {
  background: #1677FF;
  color: white;
  border: 2px solid #1677FF;
}

.social-icon.dingtalk:hover {
  background: #0958d9;
  border-color: #0958d9;
}

.social-icon.dingtalk svg {
  width: 28px;
  height: 28px;
}

.terms {
  position: fixed;
  bottom: 20px;
  left: 20px;
  right: 20px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 12px;
  color: #666;
}

.checkbox {
  color: #ff0000;
  font-size: 16px;
}

.terms-text {
  flex: 1;
}

.link {
  color: #007AFF;
  text-decoration: none;
}
</style>

